/**
 * 地图基类
 * @author shilei
 */
define("dojo/_base/declare dojo/text!./templates/Template.html dojo/_base/array dojo/_base/lang dojo/dom dojo/on dojo/dom-style dojo/dom-class dojo/query dojo/mouse dojo/topic esri/layers/FeatureLayer esri/geometry/Extent esri/geometry/Multipoint esri/layers/ArcGISTiledMapServiceLayer esri/layers/ArcGISDynamicMapServiceLayer esri/layers/GraphicsLayer esri/map esri/config esri/SpatialReference ../BaseWidget esri/dijit/InfoWindow dojo/dom-construct".split(" "),
    function (declare, template,array, lang, dom, on,domStyle,domClass,query,mouse,topic, FeatureLayer, Extent, Multipoint, ArcGISTiledMapServiceLayer,ArcGISDynamicMapServiceLayer,GraphicsLayer, Map,esriConfig, SpatialReference,BaseWidget,InfoWindow,domConstruct) {
        return declare([BaseWidget], {
            templateString:template,
            constructor:function (args) {
                this.settings = lang.mixin({mapId:"map"}, args);
                this.map = null;
                this.initMap();
            },
            /**初始化地图*/
            initMap:function () {
//            	debugger;
            	var s = this.settings;
                var mapId = s.mapId;
                var initialExtent = new Extent(ioc.map.initExt);
                var options = {
                    extent:initialExtent,
                    slider:true,
                    sliderStyle: "large",
                    sliderLabels: ioc.map.labels,
                    logo:false,
                    zoom:9
                };
                var map = new Map(mapId, options);
                var basemap = new ArcGISTiledMapServiceLayer(ioc.map.basemap,{id:"basemap"});   //部署时打开
                map.addLayer(basemap,0);
                basemap.isBasemapLayer = true;//标识为基础底图
                on(map, "load", lang.hitch(this, this.onMapLoaded));
                on(map,"layer-add",function(){
                	var lay=map.getLayer("PoiSearchLayer");
                	if(lay!=undefined){
                		map.reorderLayer(lay, 50);
                	}
                })
            },
            /**获取图形JSON对象数组*
            getGraphicsJson:function(graphics) {
                if (!graphics) {
                    return null;
                }
                var result = {};
                result.graphics = array.map(graphics, function (g) {
                    return g.toJson();
                });
                return result;
            },
            /**获取地图范围*/
            getExtent:function () {
            	console.log(this.map.extent);
                return this.map.extent;
            },
            /**设置地图范围*/
            setExtent:function (extent) {
                this.map.setExtent(extent);
            },
            /**在Silder上添加国省市*/
            addCoutryAndProvAndCityOnSlider:function(){
                var mapDIV = query("#"+this.settings.mapId,dojo.body())[0];
                //为table slider绑定的mouse Enter和Level事件
                var table = query("table",mapDIV);
                on(table, mouse.enter, lang.hitch(this, this.mouseEnterSlider));
                on(table, mouse.leave, lang.hitch(this, this.mouseLeaveSlider));
                
                //隐藏掉slider label
                var lable = query(".esriLargeSliderLabels",mapDIV)[0];
                domStyle.set(lable,"display","none");
                
                //为对应的省市区sliderLabel添加类样式
                var slideFlag = ioc.map.slideFlag;
                var slideLabels = dojo.query("div.dijitRuleLabelContainer",mapDIV);
                for (var i = 0; i < slideFlag.length; i++){
                    if(slideLabels){
                        var level =slideFlag[i];
                        var addClass = "dijitRuleLabelV" + slideFlag[i];
                        domClass.add(slideLabels[level],addClass)
                        on(slideLabels[level],"click",lang.hitch(this,this.setMapLevel,level));
                    }
                }
            },
            /**鼠标移入silder*/
            mouseEnterSlider:function(evt){
                var mapDIV = query("#"+this.settings.mapId,dojo.body())[0];
                var lable = query(".esriLargeSliderLabels",mapDIV)[0];
                domStyle.set(lable,"display","block");
            },
            /**鼠标离开slider*/
            mouseLeaveSlider:function(evt){
                var mapDIV = query("#"+this.settings.mapId,dojo.body())[0];
                var lable = query(".esriLargeSliderLabels",mapDIV)[0];
                domStyle.set(lable,"display","none");
            },
            /**设置地图级别*/
            setMapLevel:function(level,evt){
                this.map.setLevel(level);
            },
            postMixInProperties:function () {
                this.inherited(arguments);
            },
            postCreate:function () {
                this.inherited(arguments);
            },
            startup:function () {
                this.inherited(arguments);
            },
            destroy:function () {
                if (this.map) {
                    this.map.destroy();
                }
                this.inherited(arguments);
            },
        });
    });